//
//  NaviDeliveryGoodsView.m
//  one_buy_guizhou_ios
//
//  Created by xu yao on 2021/2/4.
//  Copyright © 2021 杨帅. All rights reserved.
//

#import "NaviDeliveryGoodsView.h"
#import "GAppConfig.h"
#import "UIView+Extension.h"
#import "HCToast.h"

@interface NaviDeliveryGoodsView () <CAAnimationDelegate>

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *scanButton;
@property (nonatomic, strong) UIButton *noneButton;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *confirmButton;
@property (nonatomic, copy) void (^onClockComplete)(BOOL isSuccess);
@property (nonatomic, assign) BOOL isClose;
@property (nonatomic, assign) BOOL isSuccess;

@property (nonatomic, copy) NSString *waybillNo;
@property (nonatomic, copy) NSString *orderId;

@end

@implementation NaviDeliveryGoodsView

+ (instancetype)deliveryGoodsView:(NSString *)waybillNo orderId:(nonnull NSString *)orderId superView:(nonnull UIView *)superView onClose:(nonnull void (^)(BOOL))onClockComplete {
    NaviDeliveryGoodsView *goodsView = [[NaviDeliveryGoodsView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
    goodsView.waybillNo = waybillNo;
    goodsView.orderId = orderId;
    goodsView.onClockComplete = onClockComplete;
    if (superView) {
        [superView addSubview:goodsView];
    } else {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [window addSubview:goodsView];
    }
    
    [self beginAnimation:goodsView];
    return goodsView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.backView];
    [self.backView addSubview:self.closeButton];
    [self.backView addSubview:self.textField];
    [self.backView addSubview:self.scanButton];
    [self.backView addSubview:self.noneButton];
    [self.backView addSubview:self.lineView];
    [self.backView addSubview:self.confirmButton];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.closeButton.size = CGSizeMake(44, 44);
    self.closeButton.origin = CGPointMake(0, 0);
    
    self.textField.size = CGSizeMake(self.backView.width - 32 * 2, 40);
    self.textField.origin = CGPointMake(32, 44);
    
    self.scanButton.size = self.textField.size;
    self.scanButton.origin = CGPointMake(self.textField.x, self.textField.bottom + 8);
    
    self.noneButton.size = self.textField.size;
    self.noneButton.origin = CGPointMake(self.textField.x, self.scanButton.bottom + 8);
    
    self.lineView.frame = CGRectMake(0, self.noneButton.bottom + 15, self.backView.width, 0.5);
    self.lineView.bottom = self.backView.height - 44;
    
    [self.confirmButton sizeToFit];
    self.confirmButton.centerX = self.backView.width * 0.5;
    self.confirmButton.centerY = self.lineView.bottom + 22;
}

- (void)onTapClockClick {
    self.isClose = YES;
    self.isSuccess = NO;
    [self endAnimation];
}

- (void)onTapScanClick {
    UINavigationController *vc = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    QRScanViewController *svc = [[QRScanViewController alloc] init];
    [vc pushViewController:svc animated:YES];
    __weak typeof(self) weakSelf = self;
    [svc setResultStrSuccess:^(NSString * _Nonnull resultStr) {
        weakSelf.textField.text = resultStr;
        [weakSelf onTapConfirm];
    }];
}

- (void)onTapNone {
    self.isClose = YES;
    self.isSuccess = YES;
    [self endAnimation];
}

- (void)onTapConfirm {
    if (self.textField.text.length == 0) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    //网络请求
    [NetworkTool GetHttpDataWithUrlStr:CHECKCODE Dic:@{@"orderId":self.orderId, @"waybillNo":self.waybillNo, @"validationCode":self.textField.text} SuccessBlock:^(id  _Nonnull responseObject) {
        if ([responseObject[@"code"] intValue] == 200 && [responseObject[@"data"] intValue] == 1) {
            if (weakSelf.onClockComplete) {
                weakSelf.onClockComplete(YES);
            }
        } else {
            [[HCToast shareInstance] showErrorIconToast:@"取货码错误"];
        }
    } FailureBlock:^(id  _Nonnull error) {
        NSLog(@"%@",error);
        [[HCToast shareInstance] showErrorIconToast:@"取货码错误"];
    }];
}

+ (void)beginAnimation:(NaviDeliveryGoodsView *)layerView {
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.duration = 0.25;
    //removedOnCompletion为NO保证app切换到后台动画再切回来时动画依然执行
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.fromValue = @(0.0);
    scaleAnimation.toValue = @(1.0);
    [layerView.backView.layer addAnimation:scaleAnimation forKey:@"scale-layer"];
}

- (void)endAnimation{
    [UIView animateWithDuration:0.25 animations:^{
        self.closeButton.alpha = 0;
    }];
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.duration = 0.25;
    
    //removedOnCompletion为NO保证app切换到后台动画再切回来时动画依然执行
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.fromValue = @(1.0);
    scaleAnimation.toValue = @(0.0);
    scaleAnimation.delegate = self;
    scaleAnimation.fillMode = kCAFillModeForwards;
    [self.backView.layer addAnimation:scaleAnimation forKey:@"scale-layer-end"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    [self.backView.layer removeAllAnimations];
    [self removeFromSuperview];
    if (self.onClockComplete && self.isClose) {
        self.onClockComplete(self.isSuccess);
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

#pragma mark 懒加载
- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = 8;
        _backView.frame = CGRectMake(30, 200, Screen_Width - 60, 240);
    }
    return _backView;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setTitle:@"x" forState:UIControlStateNormal];
        [_closeButton setTitleColor:[UIColor colorWithWhite:0 alpha:0.85] forState:UIControlStateNormal];
        _closeButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [_closeButton addTarget:self action:@selector(onTapClockClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.placeholder = @"请输入取货码取货";
        _textField.textColor = [UIColor colorWithWhite:0 alpha:0.85];
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.layer.masksToBounds = YES;
        _textField.layer.cornerRadius = 5;
        _textField.layer.borderColor = [UIColor colorWithRed:151.0/255.0 green:151.0/255.0 blue:151.0/255.0 alpha:1].CGColor;
        _textField.layer.borderWidth = 1;
        _textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _textField;
}

- (UIButton *)scanButton {
    if (!_scanButton) {
        _scanButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_scanButton setTitle:@"点击扫码取货" forState:UIControlStateNormal];
        [_scanButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _scanButton.titleLabel.font = [UIFont systemFontOfSize:18];
        _scanButton.backgroundColor = [UIColor colorWithRed:1 green:71.0/255.0 blue:0 alpha:1];
        _scanButton.layer.masksToBounds = YES;
        _scanButton.layer.cornerRadius = 5;
        [_scanButton addTarget:self action:@selector(onTapScanClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _scanButton;;
}

- (UIButton *)noneButton {
    if (!_noneButton) {
        _noneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_noneButton setTitle:@"无凭证取货" forState:UIControlStateNormal];
        [_noneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _noneButton.titleLabel.font = [UIFont systemFontOfSize:18];
        _noneButton.backgroundColor = [UIColor colorWithRed:1 green:71.0/255.0 blue:0 alpha:1];
        _noneButton.layer.masksToBounds = YES;
        _noneButton.layer.cornerRadius = 5;
        [_noneButton addTarget:self action:@selector(onTapNone) forControlEvents:UIControlEventTouchUpInside];
    }
    return _noneButton;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithRed:60.0/255.0 green:60.0/255.0 blue:67.0/255.0 alpha:1];
    }
    return _lineView;
}

- (UIButton *)confirmButton {
    if (!_confirmButton) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor colorWithRed:49.0/255.0 green:123.0/255.0 blue:1 alpha:1] forState:UIControlStateNormal];
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_confirmButton addTarget:self action:@selector(onTapConfirm) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}

@end
